package com.itsm.statemachine.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;

public class TransitionEventPublisher {
    private static final List<TransitionEventListener> listeners = new ArrayList<>();

    public static void addListener(TransitionEventListener listener) {
        listeners.add(listener);
    }

    public static void removeListener(TransitionEventListener listener) {
        listeners.remove(listener);
    }

    public static <S, E, C> void publishEvent(TransitionEvent<S, E, C> event, ThreadPoolExecutor executorService) {
        if(!listeners.isEmpty()){
            Runnable eventTask = () -> {
                for (TransitionEventListener listener : listeners) {
                    listener.onEvent(event);
                }
            };
            executorService.submit(eventTask);
        }
    }
}
